package com.example.demo.zaqizabatest.jdkproxy;

import com.example.demo.zaqizabatest.common.MyTransaction;
import com.example.demo.zaqizabatest.common.PersonService;
import com.example.demo.zaqizabatest.common.PersonServiceImpl;

import java.lang.reflect.Proxy;

/**
 * @ClassName Test
 * @Author lys4134
 * @Date 2021/08/12 10:01:07
 * @Description Test
 * @Version 1.0
 **/
public class Test {
    public static void main(String[] args) {
        HelloWorld helloWorld = new HelloWorld();
//        MyTransaction myTransaction = new MyTransaction();
        LoggerHandler loggerHandler = new LoggerHandler(helloWorld);

        IHelloWorld iHelloWorld = (IHelloWorld) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), helloWorld.getClass().getInterfaces(), loggerHandler);
        iHelloWorld.sayHello();
        Object target = new PersonServiceImpl();
        MyTransaction myTransaction = new MyTransaction();
        PersonServiceInterceptor interceptor = new PersonServiceInterceptor(target, myTransaction);
        PersonService personService = (PersonService) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), interceptor);
        String returnValue = (String) personService.savePerson();
        System.out.println(returnValue);

    }
}
